package cn.idu;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class ExeractVideoTest {
    public static void main(String[] args) {
        File file = new File("./");

        plistFile(file);

    }

    private static void plistFile(File file) {
        if (file.isFile()) {
            exeractVideo(file);
        } else {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                plistFile(files[i]);
            }
        }
    }

    private static void exeractVideo(File file) {
        String name = file.getName();
        println(name);
        if (name.endsWith(".mp4")) {
            String command = "ffmpeg -r 10 -i " + file.getAbsolutePath() + " " + name.substring(0, name.length() - 4) + "_%5d.jpg";
            try {

                Process process = Runtime.getRuntime().exec(command);
                process.waitFor();
                ExecOutput(process);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean ExecOutput(Process process) throws Exception {
        if (process == null) {
            return false;
        } else {
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            String output = "";
            while ((line = input.readLine()) != null) {
                output += line + "\n";
            }
            input.close();
            ir.close();
            if (output.length() > 0) {
                println(output);
            }
        }
        return true;
    }


    public static void println(String s) {
        System.out.println(s);
    }
}
